//
//  PointHistoryTableViewCell.swift
//  ezyform
//
//  Created by MoennigQ on 26/12/19.
//  Copyright © 2019 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit

class PointHistoryTableViewCell: UITableViewCell {

    let titleLabel = ContentLabel(R.font.interMedium(size: 15))
    let dateLabel = BaseLabel()
    let amountLabel = BaseLabel()
    let lineView = View(hexStr: Constant.SeparatorColorHexStr)
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpviews()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    func setUpviews(){
        self.selectionStyle = .none
        self.contentView.backgroundColor = .white
        //amount
        amountLabel.setFont(R.font.interBold(size: 14))
        amountLabel.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        amountLabel.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        self.contentView.addSubview(amountLabel)
        amountLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-18)
            make.centerY.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)
            
        }
        
        //title
        titleLabel.numberOfLines = 0
        titleLabel.adjustsFontSizeToFitWidth = true
        self.contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(12)
            make.right.equalTo(amountLabel.snp.left).offset(-5)
            make.left.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        //date
        dateLabel.setFont(R.font.interMedium(size: 12))
        dateLabel.textColor = UIColor(hexStr: "#292D32B3")
        self.contentView.addSubview(dateLabel)
        dateLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.left.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)
            make.bottom.equalTo(-12)
            
            
        }
        //line view
        self.contentView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.right.left.equalToSuperview()
            make.height.equalTo(1)
            make.bottom.equalToSuperview()
        }
    }
    func setData(_ row: Int, list: [PointModel]){
        let model = list[row]
        self.titleLabel.text = model.note
        if let createAtDate = model.created_at {
            self.dateLabel.text = DateUtil().convertDateStrToDateStr(dateStr: createAtDate, oldFormat: "MMM d yyyy HH:mm:ss", newFormat: "dd/MM/yyyy")
        }
        
        //如果是减分就是红色的，加分就是
        if let data = model.point {
            let currentPoints = NumberFormatter.localizedString(from: NSNumber(value: data), number: .decimal)
            
            if data >= 0 {
                self.amountLabel.text = "+" + currentPoints
                self.amountLabel.textColor = UIColor(hexStr: "#379657")
            }else{
                self.amountLabel.text = currentPoints
                self.amountLabel.textColor = UIColor(hexStr: "#DD890B")
            }
        }
        
        if row == list.count - 1 {
            self.lineView.isHidden = true
        }else{
            self.lineView.isHidden = false
        }
    }
}
